this.name                                        = "Player Fuel Station";
this.author                                        = "Thargoid, modified by Lestradae";
this.copyright                                = "Creative Commons: attribution, non-commercial, sharealike.";
this.description                                = "Script for fuel station";
this.version                                = "1.2";

this.shipSpawned = function()
        { //  line the station up with the route one. Code borrowed from Anarchies for convenience.
        var targetVector = system.mainPlanet.position.subtract(this.ship.position).direction();
        var angle = this.ship.heading.angleTo(targetVector);
        var cross = this.ship.heading.cross(targetVector).direction();
        if (oolite.compareVersion('1.73') <= 0)
                {
                this.ship.orientation = this.ship.orientation.rotate(cross, -angle);
                }
        else
                {
                this.ship.setOrientation(this.rotateQ(this.ship.orientation, cross, -angle));
            }

    var traders = system.shipsWithPrimaryRole("trader", this.ship, 50E3)
    if(traders.length>0 && traders[0].AIState == "HEAD_FOR_PLANET")
        {
            traders[0].script.checkFuelStationDistance = this.checkFuelStationDistance; // attach function to script.
            traders[0].setAI("fuelStation_gotoFuelStationAI.plist");
        }
        }

this.rotateQ = function(q, axis, angle)
        {
        return q.rotate(axis, angle)
        }

this.playerDetected = function() {
	this.firstMessage = true;
	this.fuelTransferred = 0;
	if (this.scanTimer) {
		this.scanTimer.start();
	} else {
		this.scanTimer = new Timer(this, this.locatePlayer, 0, 0.50);
	}
};

this.locatePlayer = function()
        {
        if(!this.ship || !this.ship.position) // if the ship no longer exists but timer is running, e.g. if player has jumped whilst near a satellite into a system without one
                {
                this.playerGone();
                return;
                }

        if(this.ship.position.distanceTo(player.ship.position) < 150) // player ship within 150m of the centre of the fuel station
                {
                if(player.ship.fuel < 7)
                        {
                        if(player.credits > 0.3)
                                { // player in place, fuel tank not full and credit balance not empty
                                player.ship.fuel += 0.1;
                                this.fuelTransferred += 0.1;
                                player.credits -= 0.0;
                                if(this.firstMessage)
                                        {
                                        player.consoleMessage("Fuel transfer is underway, please come to a halt.", 6);
                                        this.firstMessage = false;
                                        }
                                return;
                                }
                        else
                                { // not enough funds left
                                player.consoleMessage("Insufficient credits remaining, transfer terminated.", 6);
								if(this.scanTimer) {
									this.scanTimer.stop();
								}
                                return;
                                }
                        }
                else
                        { // Fuel tank full
                        this.fuelBill = this.fuelTransferred * 0;
                        player.consoleMessage("Fuel tanks are full.", 6);
						if(this.scanTimer) {
							this.scanTimer.stop();
						}
                        return;

                        }
                }
        }

this.shipTraversePositiveZ = this.shipTraverseNegativeZ = function(ship)
        {
        if(ship.isPlayer) this.playerLeaving()
        }

this.playerLeaving = function()
        {
        this.fuelTransferred = (Math.floor(10 * this.fuelTransferred)) / 10;
        this.fuelBill = (Math.floor(0 * this.fuelTransferred)) / 10;
        player.consoleMessage("Summary - " + this.fuelTransferred + " ly transferred, " + this.fuelBill + " credits charged.", 6);
        this.fuelTransferred = 0;
        this.fuelBill = 0;
        this.firstMessage = true;
        }

this.playerWillEnterWitchspace = function() {
    if (this.scanTimer) {
		this.scanTimer.stop();
		delete this.scanTimer;
	}
}

this.shipDied = function(whom, why)
        {
        this.playerGone();

        if(whom && whom.isPlayer)
                {
                this.fuelCount = (Math.ceil(Math.random() * 30) + 30);
                this.ship.spawn("fuelStation_burningFuel", this.fuelCount); // lets make this go with a bang!
                player.consoleMessage("CCTV beam towards the main station detected.", 6);
                player.score -=1; // don't condone vandalism!
                player.bounty += 20;
                }
        }


// below not used by stationscript but is attached to traderscript.
this.checkFuelStationDistance = function()
        {
        if(this.ship.position.distanceTo(this.ship.target.position) > 50E3)
                {
                this.ship.reactToAIMessage("NEXT_FUELSTATION")
                }
        }

this.checkUsage = function()
        {
         if(this.ship.target.position.distanceTo(player.ship.position) < 1000)
                {
                this.ship.reactToAIMessage("NEXT_FUELSTATION")
                }
        else
                {
                this.ship.reactToAIMessage("STATION_CLEAR")
                }
        }